/*
    Project:        Home Power Control
    File:           HPCSensorValue.h
    Author:         christian@karunamusic.at
    Date:           01.2015
    Description:
 */

#ifndef HPCSENSORVALUE_H
#define HPCSENSORVALUE_H

#include <QList>
#include "HPCValue.h"
#include "HPCAlarmValue.h"

class HPCSensorValue : public HPCValue
{
    Q_OBJECT
public:
    /** \brief Constructor.
     */
    HPCSensorValue();
    /** \brief Constructor.
     *
     * \param val The value as QVariant.
     */
    HPCSensorValue( const QVariant& val );
    /** \brief Copy Constructor.
     *
     * \param ref The object to copy.
     */
    HPCSensorValue( HPCSensorValue& ref );
    /** \brief Destructor.
     */
    virtual ~HPCSensorValue();

    /** \brief Get he value from Min-Alarm-vector at position ndx.
     * The index is an argument of the HPCSensor::TSignalAlarm callback method!
     *
     * \param ndx The index of the value.
     * \return A pointer to the value or 0 if index out of range.
     */
    const HPCValue* Min( int ndx );
    /** \brief Get he value from Max-Alarm-vector at position ndx.
     * The index is an argument of the HPCSensor::TSignalAlarm callback method!
     *
     * \param ndx The index of the value.
     * \return A pointer to the value or 0 if index out of range.
     */
    const HPCValue* Max( int ndx );

    /** \brief Add upper and lower alarm to the SensorValue with the given id.
     * Can be called more than once to add alarms.
     *
     * \param Min The min-value as string or NULL if unused.
     * \param Max The max-value as string or NULL if unused.
     */
    void addAlarm( const QString& Min, const QString& Max );

    /** \brief Assignment operator.
     * Check also if an alarm should be signalled.
     *
     * \param v The object to copy.
     * \return HPCSensorValue.
     */
    HPCSensorValue& operator = ( const HPCValue& v );

    friend QDataStream& operator<< ( QDataStream& os, const HPCSensorValue& v );
    friend QDataStream& operator>> ( QDataStream& is, HPCSensorValue& v );

signals:
    /** \brief Signals an alarmed value.
     *
     * \param val Alarmed value.
     * \param gt true = Max, false = Min alarm.
     * \param ndx the index in the alarm-vector.
     */
    void alarmed( HPCSensorValue& val, bool gt, unsigned int ndx );

protected:
    /** \brief method to check if sensor is beyond limits.
     *
     * \return int 0 if no alarm, <0 if below limit, >0 if above limit (return value - 1 = index of the fired alarm).
     */
    int isAlarm();

    QList<HPCAlarmValue *> m_Min;
    QList<HPCAlarmValue *> m_Max;
};

QDataStream& operator<< ( QDataStream& os, const HPCSensorValue& v );
QDataStream& operator>> ( QDataStream& is, HPCSensorValue& v );

#endif // HPCSENSORVALUE_H
